// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    //轮播图的数组
    backgroundArr: [],
    //轮播图当前的下标
    current: 0,
    //是否自动播放轮播图
    autoplay: false,
    flag: true,
    list: [],
    goods_id: '',
    collect: '',
    // flag: 1
  },

  //监听轮播图的下标
  monitorCurrent: function (e) {
    // console.log(e.detail.current)
    let current = e.detail.current;
    this.setData({
      current: current
    })
  },

  home() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  car() {
    wx.switchTab({
      url: '/pages/car/car',
    })
  },
  join_car() {
    this.setData({
      flag: false
    })
  },
  // 加入购物车
  sure() {
    this.setData({
      flag: true
    })
    wx.request({
      url: `${baseurl}/good.ShoppingCart/cart_add`,
      method: 'POST',
      data: {
        goods_id: this.data.goods_id,
        number: 1,
        user_id: wx.getStorageSync("user_id"),
        openid: wx.getStorageSync("openid")
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('222',options);
    // 详情列表
    this.setData({
      goods_id: options.id
    })
    console.log(this.data.goods_id);
    this.list(this.data.goods_id)
  },

  // 详情
  list(id) {
    wx.request({
      url: `${baseurl}/good.ExchangeShop/good_details`,
      method: 'POST',
      data: {
        goods_id: id,
        user_id: wx.getStorageSync("user_id"),
      },
      success: (res) => {
        // console.log(res.data.data);
        console.log('list:', res.data.data.collect);
        if (res.data.data.collect == null) {
          this.setData({
            flag: 1
          })
        } else {
          this.setData({
            flag: 2
          })
        }
        const CONTENT = res.data.data.data.content.replace(
          /\<img/gi,
          '<img style="width:100%;height:auto" '
        );
        this.setData({
          list: res.data.data,
          // backgroundArr: res.data.data.data.image,
          // backgroundArr: res.data.data.data.image,
          descript:CONTENT,
          collect: res.data.data.collect

        })
      }
    })
  },
  // 添加收藏
  collet() {
    var that = this
    if (this.data.collect == null) {
      console.log('111', this.data.collect);
      // 添加
      wx.request({
        url: `${baseurl}/user.UserCollect/collect_add`,
        method: 'POST',
        data: {
          collect_id: this.data.goods_id,
          type: 2,
          user_id: wx.getStorageSync("user_id"),
          openid: wx.getStorageSync("openid"),
        },
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          that.list(that.data.goods_id)
        }
      })

    } else {
      console.log('222', this.data.collect);

      wx.request({
        url: `${baseurl}/user.UserCollect/collect_del`,
        method: 'POST',
        data: {
          user_id: wx.getStorageSync("user_id"),
          id: this.data.collect,
        },
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          that.list(that.data.goods_id)
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //开启轮播图
    this.setData({
      autoplay: true
    })

    // this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },
})
